package org.demo.algorithm.leetcode.linked;

import org.demo.algorithm.common.ListNode;

public class IntersectNode {

    public ListNode getIntersectNode(ListNode listA, ListNode listB) {
        ListNode headA = listA;
        ListNode headB = listB;

        while (headA != headB) {
            if (headA == null) {
                headA = listB;
            } else {
                headA = headA.next;
            }
            if (headB == null) {
                headB = listA;
            } else {
                headB = headB.next;
            }
        }
        return headA;
    }

}
